package com.sa.app.fragments;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.sa.app.R;
import com.sa.app.activities.MainActivity;
import com.sa.app.objects.Region;
import com.sa.app.utilities.CustomizedOverlay;
import com.sa.app.utilities.MapCircleOverlay;
import com.sa.app.utilities.MyApplication;

public class MapFragment extends Fragment {

	private MapView map = null;
	private MyLocationOverlay me = null;
	private Boolean isFirstTime = true;
	private View view = null;
	private Context context;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.context = inflater.getContext();
		// this.context=getActivity().getBaseContext();
		if (map == null) {
			// La unica manera rapida que se me ha ocurrido es esta, que cada
			// uno se descomente su linea y comente la otra cuando vaya a
			// utilizar algo del mapa
			// Key API ALE
			map = new MapView(getActivity(),
					"0mjABpkE4XqGtcWzFV8pRyFFqph0tSPL2JBGMaA");
			// Key API VACAS
			// map = new
			// MapView(getActivity(),"0cnkc5rmKBCipb70f9XJ14PcdiCTuzp5TCxs-_Q");

		}

		generateRegionsForMap();

		if (view == null) {
			view = inflater.inflate(R.layout.map_view, null);
		}
		if (((ViewGroup) view.getParent()) != null) {
			((ViewGroup) view.getParent()).removeView(view);
		}



		return view;

	}

	// Esto hace que no cuelgue la aplicación al cambiar de activity utilizando
	// la libreria de compatibilidad
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		setUserVisibleHint(true);
	}

	@Override
	public void onResume() {
		super.onResume();

		GeoPoint center = new GeoPoint(((int) (((MyApplication) getActivity()
				.getApplication()).getLocation().getLatitude() * 1E6)),
				(int) (((MyApplication) getActivity().getApplication())
						.getLocation().getLongitude() * 1E6));
		map.getController().setCenter(center);

	}

	@Override
	public void onPause() {
		super.onPause();

	

		LayoutInflater factory = getActivity().getLayoutInflater();
		View vf = factory.inflate(R.layout.actionbarview, null);

		ActionBar bar = getActivity().getActionBar();
		bar.setDisplayShowTitleEnabled(false);
		TextView title = (TextView) vf.findViewById(R.id.act_title);

		((ViewGroup) title.getParent()).removeView(title);
		bar.setDisplayShowCustomEnabled(true);
		bar.setCustomView((View) title);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		map.setClickable(true);
		GeoPoint center = new GeoPoint(0, 0);
		map.getController().setCenter(center);
		map.getController().setZoom(15);
		map.setBuiltInZoomControls(true);

		Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);

		marker.setBounds(0, 0, marker.getIntrinsicWidth(),
				marker.getIntrinsicHeight());

		me = new MyLocationOverlay(getActivity(), map);
		map.getOverlays().add(me);

		if (isFirstTime) {
			((ViewGroup) getView()).addView(map);
			view = getView();
			isFirstTime = false;
		}
	}

	public void update(ArrayList<OverlayItem> overlays) {

		map.getOverlays().clear();
		generateRegionsForMap();
	}

	private void generateRegionsForMap() {
		List<Region> list = new ArrayList<Region>();
		list.addAll(((MainActivity) getActivity()).getRegionsList());
		List<Overlay> mapOverlays = map.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
		CustomizedOverlay itemizedoverlay = new CustomizedOverlay(drawable,
				context, getActivity().getApplication(),
				((MainActivity) getActivity()).getSectionsPagerAdapter()
						.getMapFragment());

		for (Region r : list) {
			GeoPoint point = new GeoPoint(r.getLatitude(), r.getLongitude());
			MapCircleOverlay om = new MapCircleOverlay(point, r.getRadio(),
					r.getName());
			List<Overlay> capas = map.getOverlays();
			capas.add(om);
			OverlayItem overlayitem = new OverlayItem(point, "casa",
					"programando...");
			itemizedoverlay.addOverlay(overlayitem);
			mapOverlays.add(itemizedoverlay);
		}
		map.postInvalidate();
	}

}